package cn.com.wanywhn.a2017_04_02_bmob_brvah;

import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.Date;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.com.wanywhn.a2017_04_02_bmob_brvah.Bean.PostListBean;

@EActivity(R.layout.activity_add_post_list_item)
public class AddPostListItem extends BaseActivityToolBar{

    //TODO 修改页面
    @ViewById(R.id.AddPostListItemTitle)
    EditText title;
    @ViewById(R.id.AddPostListItemDescrip)
    EditText describe;
    @AfterViews
    void init(){
        setVisiableBackBtn();
        setVisiableToolBar("添加");

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.AddPostListItemOK:
                        final PostListBean plb=new PostListBean();
                        plb.setTitle(title.getText().toString());
                        plb.setOwner(BmobUser.getCurrentUser());
                        plb.setContext(describe.getText().toString());
                        plb.setCreatetime((new Date(System.currentTimeMillis())).toString());
                        plb.setLastreply("");
                        plb.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null) {

                                    Toast.makeText(AddPostListItem.this, "成功!!", Toast.LENGTH_SHORT).show();
                                    AddPostListItem.this.setResult(RESULT_OK,getIntent().putExtra("ADD", (Parcelable) plb));
                                    AddPostListItem.this.finish();
                                }else {
                                    System.err.println(e.toString());
                                }
                            }
                        });
                }

                return true;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_post_list_item,menu);
        return true;
    }


}
